package net.kts.subskill.conversation;

import java.util.ArrayList;
import java.util.List;



import net.kts.define.SQLiteDatabaseStr;
import net.kts.main.R;
import net.kts.setting.language.LanguageUtil;
import net.kts.subskill.adapter.SubskillConversationAdapter;
import net.kts.subskill.conversation.dialog.ExpandConversationDig;
import net.kts.subskill.conversation.model.Conversation;
import net.kts.subskill.conversation.sqlite.ConversationDaoSQLite;
import net.kts.transrate.TranslateService;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;



public class SubskillConversation extends Activity {

	
	private SubskillConversationAdapter conversationAdapter = null;
	private List<Conversation> strList = new ArrayList<Conversation>();
	private ListView listView = null;
	
	private ConversationDaoSQLite conversationDaoSQLite = null;
	
	private final Conversation saveConversation = new Conversation();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subskill_conversation);
		
		
		
		Button button = (Button)findViewById(R.id.subskill_translate_btn);
		this.conversationAdapter = new SubskillConversationAdapter(this, R.layout.subskill_conversation, strList);
		this.listView = (ListView)findViewById(R.id.subskill_conversation_listview);
		final EditText targetEdit = (EditText)findViewById(R.id.transTarget_EditText);
		final EditText transEdit = (EditText)findViewById(R.id.trans_EditText);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
			
				Conversation conversation = (Conversation) v.getTag();
				
				saveConversation.setSeq(conversation.getSeq());
				saveConversation.setKo(conversation.getKo());
				saveConversation.setLang(conversation.getLang());
				
				showDialog(1, null);
			}
		});
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View v,
					int position, long arg3) {
				
				Bundle bundle = new Bundle();
				
				Conversation conversation = (Conversation) v.getTag();
				
				saveConversation.setSeq(conversation.getSeq());
				saveConversation.setKo(conversation.getKo());
				saveConversation.setLang(conversation.getLang());
				
//				bundle.putInt("position", position);
//				bundle.putSerializable("conversation", conversation);
//				
//				Log.d("kts", ""+position);
				
				//showDialog(2, bundle);
				showDialog(2, null);
				
				return false;
			}
		});
		
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
				//button.setBackgroundDrawable(d)
				
				String transTarget = targetEdit.getText().toString();
				String trans = TranslateService.translateToKorean(transTarget);
				
				transEdit.setText(trans);
//				
				
				saveConversation.setLang(transTarget);
				saveConversation.setKo(trans);
//				
				conversationDaoSQLite.insertConversation(saveConversation, LanguageUtil.getCurrentLanguage());
				
				strListInit();
			
//				Bundle bundle = new Bundle();
//				bundle.putSerializable("conversation", conversation);
				
//				showDialog(1, null);
			}
		});
		
	}
	
	
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle bundle) {
		// TODO Auto-generated method stub
		
		
		
		
		switch (id) {
		case 1:
			
		ExpandConversationDig dialog = new ExpandConversationDig(this);
		dialog.setMessage(saveConversation.getKo());
		
		dialog.show();
//		dialog.setm
			
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);			
//			builder.setMessage(saveConversation.getLang());
//			builder.setTitle(saveConversation.getKo());
//			//builder.setIcon(R.drawable.icon);									
//			return builder.create();		

			break;
		case 2 :
			

			
			
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);			
			builder2.setMessage("Delete?");
//			builder2.setTitle("Delete?");
//			builder2.setIcon(R.drawable.icon);
			
			builder2.setPositiveButton("YES", 
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {							
							// TODO Auto-generated method stub
//							Toast.makeText(getApplicationContext(), ""+saveConversation.getSeq(), Toast.LENGTH_SHORT).show();
							
							conversationDaoSQLite.deleteConversation(saveConversation);
							
							strListInit();
							
						}
					});
			
			builder2.setNegativeButton("NO", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
					
				
			});
			
			
			//builder2.setCancelable(false);// ���̾�α� â�� ���� �ʴ´�.
			//builder2.setNeutralButton(arg0, arg1)
			return builder2.create();
			
		default:
			break;
		}
		
		
		
		return super.onCreateDialog(id, bundle);
	}


	private void strListInit() {
		// TODO Auto-generated method stub
		
		strList.removeAll(strList);
		
		List<Conversation> conversationList = this.conversationDaoSQLite.selectConversation(LanguageUtil.getCurrentLanguage());
		
		for(Conversation conversation : conversationList) {
			
			if(conversation.getLang() == null || conversation.getLang().trim().equals("")) {
				continue;
			}
			
			strList.add(conversation);
		}
		
		
				
		listView.setAdapter(this.conversationAdapter);
	}




	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		
		this.conversationDaoSQLite = new ConversationDaoSQLite(this, SQLiteDatabaseStr.dbFileName, null, 1);
		
		strListInit();
		
	}



	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		this.conversationDaoSQLite.close();
	}
	
	

}
